package com.ithima.domin;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentEntry001 {
    public static void main(String[] args) {
        //创建一个Student类的集合
        ArrayList<Student> list = new ArrayList<>();
        //键盘录入
        Scanner sc = new Scanner(System.in);
        //死循环
        while (true) {
            System.out.println("----------" + "欢迎来到学生管理系统" + "----------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.再见");
            System.out.println("请输入你的选择");
            String zyl = sc.next();
            //switch 语句
            switch (zyl) {
                case "1":
                    System.out.println("添加学生");
                    tianJianStudent(list);
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    System.out.println("修改学生");
                    xiuGaiStudent(list);
                    break;
                case "4":
                    System.out.println("查看学生");
                    chanKanStudent(list);
                    break;
                case "5":
                    System.out.println("再见");
                    System.exit(0);
                default:
                    System.out.println("你输入的信息有误,请重新输入");
            }
        }

    }

    //修改学生信息
    private static void xiuGaiStudent(ArrayList<Student> list) {
        //键盘录入学号
        Scanner sc = new Scanner(System.in);
        //先集合索引==0,返回
        if (list.size() == 0) {
            return;
        }
        System.out.println("请输入要修改的学号:");
        String xuahao;
        //死循环
        while (true) {
            String xuehao = sc.next();
            //调用getIndex方法
            int index = getIndex(list, xuehao);
            //判断索引是否==-1,等于的话,学号已经存在,不等与的话,继续修改,结束
            if (index == -1) {
                System.out.println("你输入的学号已经存在");
            } else {
                System.out.println("请输入姓名:");
                String name = sc.next();
                System.out.println("请输入年龄:");
                int age = sc.nextInt();
                System.out.println("请输入生日:");
                String shengri = sc.next();
                //创建学生对象
                Student student = new Student(xuehao,name,age,shengri);
                //用修改方法修改索引和学生对象
                list.set(index,student);
                System.out.println("修改成功");
                break;

            }

        }
    }

    //删除学生信息
    private static void deleteStudent(ArrayList<Student> list) {
        //键盘录入要查找的学号
        Scanner sc = new Scanner(System.in);
        //先判断集合索引==0,返回
        if (list.size() == 0) {
            return;
        }
        //死循环
        System.out.println("请输入你要删除的学号:");
        String xuehao = null;
        while (true) {
            xuehao=sc.next();
            //调用getIndex方法
            int index = getIndex(list, xuehao);
            //如果索引==-1,那么输入的学号不存在,不是的话删除成功,结束循环
            if (index == -1) {
                System.out.println("学号不存在,请重新输入");
            } else {
                list.remove(index);
                System.out.println("删除成功");
                break;
            }
        }
    }

    //查看学生信息
    private static void chanKanStudent(ArrayList<Student> list) {
        //遍历数组,看看有没有学生信息
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
        }
        //如果索引长度<=0,那么暂无学生信息,返回
        if (list.size() <= 0) {
            System.out.println("暂无学生信息,请添加数据");
            return;
        }
        //显示表头信息
        System.out.println("学号\t姓名\t年龄\t生日");
        //遍历数组
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
            Student student = list.get(i);
            //打印学生对象里面的变量
            System.out.println(student.getXuehao() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getShengri());
        }
    }

    //添加信息
    private static void tianJianStudent(ArrayList<Student> list) {
        //用键盘录入添加学生
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        //设置Sting类型的学号
        String xuehao;
        //死循环
        while (true) {
            xuehao = sc.next();
            //调用判断学号是否存在的方法
            int index = getIndex(list, xuehao);
            //判断如果索引>=0,那么就是学号已经存在.不是的话结束判断
            if (index >= 0) {
                System.out.println("你输入的学号已经存在,请重新输入:");
            } else {
                break;
            }
        }
            System.out.println("请输入姓名:");
            String name = sc.next();
            System.out.println("请输入年龄:");
            int age = sc.nextInt();
            System.out.println("请输入生日");
            String shengri = sc.next();
            //创建学生对象
            Student student = new Student(xuehao,name,age,shengri);
            //用添加的方法到集合中
            list.add(student);
            System.out.println("添加成功");
        }


    //判断学号是否存在的方法
    public static int getIndex(ArrayList<Student> list, String xuehao) {
        //定义一个索引值为-1
        int index = -1;
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
            //如果键盘录入的学号==已经录入的学号
            if (xuehao.equals(list.get(i).getXuehao())) {
                //那么索引值=i
                index = i;
                break;
            }
        }
        return index;
    }
}